//
//  YouBeautyTabBarBaseViewController.m
//  YouBeauty
//
//  Created by Kelly xu on 2019/4/4.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "YouBeautyTabBarBaseViewController.h"
#import "RDVTabBarItem.h"
@interface YouBeautyTabBarBaseViewController(){
    
}
@end
@implementation YouBeautyTabBarBaseViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = Color(@"fafafa");
    [[UITabBar appearance] setBackgroundColor:[UIColor clearColor]];
    [self.tabBar setShadowImage:[UIImage new]];
    [self addChildViewControllers];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tabBarHidden:) name:@"tabBarHidden" object:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
}
//修改tabbar高度
- (void)viewWillLayoutSubviews {
    CGRect tabFrame = self.tabBar.frame;
    tabFrame.size.height = 76;
    tabFrame.origin.y = DEVICE_HEIGHT - 76 - BottomSafeAreaHeight;
    self.tabBar.frame = tabFrame;
    
}

-(void)addChildViewControllers{
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabbarBg1"]];
    UIViewController *firstViewController = [[UIViewController alloc]init];
    [self addChildViewController:firstViewController title:@"发现" imageName:@"finder"];
    UIViewController *secondViewController = [[UIViewController alloc]init];
    [self addChildViewController:secondViewController title:@"美光" imageName:@"myBeauty"];
    UIViewController *thirdViewController = [[UIViewController alloc]init];
    [self addChildViewController:thirdViewController title:@"我的" imageName:@"myCenter"];
}
-(void)addChildViewController:(UIViewController *)childController title:(NSString*)title imageName:(NSString*)imageName{
    childController.tabBarItem.title = title;
    UIEdgeInsets insets;
    insets.bottom = 5;
    childController.tabBarItem.imageInsets = insets;
    [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:Color(@"7E8080"),NSFontAttributeName:[UIFont systemFontOfSize:11]} forState:UIControlStateNormal];
    [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:Color(@"7E8080"),NSFontAttributeName:[UIFont systemFontOfSize:11]} forState:UIControlStateSelected];
    childController.tabBarItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_N",imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%@_H",imageName]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    RTContainerNavigationController *nav = [[RTContainerNavigationController alloc]initWithRootViewController:childController];
    [self addChildViewController:nav];
}
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    if ([item.title isEqualToString:@"发现"]) {
        [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabbarBg1"]];
    }else if ([item.title isEqualToString:@"美光"]) {
        [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabbarBg2"]];
    }else if ([item.title isEqualToString:@"我的"]) {
        [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabbarBg3"]];
    }
}
-(void)tabBarHidden:(NSNotification*)notifyObj{
    BOOL hidden = [notifyObj.object boolValue];
//    [self setTabBarHidden:hidden animated:YES];
}
@end
